package mylogger

import (
	"errors"
	"fmt"
	"strings"
	"time"
)

type logLevel uint16

const (
	UNKNOWN logLevel = iota
	TRACE
	DEBUG
	INFO
	WARNING
	ERROR
	FATAL
)
type Logger struct {
	Level logLevel
}

func parseLogLevel(s string) (logLevel, error) {
	s = strings.ToLower(s)
	switch s {
	case "debug":
		return DEBUG, nil
	case "trace":
		return TRACE, nil
	case "info":
		return INFO, nil
	case "warning":
		return WARNING, nil
	case "error":
		return ERROR, nil
	case "fatal":
		return FATAL, nil
	default:
		err := errors.New("invalid level")
		return UNKNOWN, err

	
	}
}

// Newlog mylog
func Newlog(levelstr string) Logger {
	level , err := parseLogLevel(levelstr)
	if err != nil{
		panic(err)
	}
	return Logger{
		Level: level,
	}
}

func (l Logger)enable(loglevel logLevel) bool {
	return loglevel >= l.Level
}

// Debug myDebug
func (l Logger)Debug(msg string)  {
	if l.enable(DEBUG) {
		now := time.Now()
		fmt.Printf("[%s] [DEBUG] %s\n", now.Format("2006-01-02 15:04:05"), msg)
	}

}

func (l Logger)Info(msg string)  {
	if l.enable(INFO) {
		now := time.Now()
		fmt.Printf("[%s] [INFO] %s\n", now.Format("2006-01-02 15:04:05"), msg)
	}

}

func (l Logger)Error(msg string)  {
	if l.enable(ERROR) {
		now := time.Now()
		fmt.Printf("[%s] [ERROR] %s\n", now.Format("2006-01-02 15:04:05"), msg)
	}

}

func (l Logger)Warning(msg string)  {
	if l.enable(WARNING) {
		now := time.Now()
		fmt.Printf("[%s] [WARNING] %s\n", now.Format("2006-01-02 15:04:05"), msg)
	}

}

func (l Logger)Fatal(msg string)  {
	if l.enable(FATAL) {
		now := time.Now()
		fmt.Printf("[%s] [FATAL] %s\n", now.Format("2006-01-02 15:04:05"), msg)
	}

}

